package com.azkj.album.network;


import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.azkj.album.MyApplication;
import com.azkj.album.dto.LoginBean;
import com.azkj.album.network.utils.StoreUtils;

import java.util.List;


public class ConfigStorage {
    private static final String TAG = ConfigStorage.class.getSimpleName();

    private static final String KEY_USER_INFO = "key_user_info";
    private static final String KEY_VISITOR_INFO = "key_visitor_info";
    private static final String KEY_TOKEN = "key_token";
    private static final String KEY_SEARCH_HISTORY = "key_search_history";


    private static ConfigStorage configStorage;
    private Context mContext;

    public static ConfigStorage getInstance() {
        if (configStorage == null) {
            configStorage = new ConfigStorage();
        }
        return configStorage;
    }


    private ConfigStorage() {
        mContext = MyApplication.getApplication();
    }


    /**
     * 用户信息
     *
     * @return
     */
    public LoginBean getUserBean() {
        String json = (String) StoreUtils.get(KEY_USER_INFO, null);
        return JSON.parseObject(json, LoginBean.class);
    }

    /**
     * 游客信息
     *
     * @return
     */
    public LoginBean getVisitorBean() {
        String json = (String) StoreUtils.get(KEY_VISITOR_INFO, null);
        return JSON.parseObject(json, LoginBean.class);
    }

    public String getToken() {
        return (String) StoreUtils.get(KEY_TOKEN, null);
    }


    public void saveUserBean(LoginBean bean) {
        if (bean != null) {
            StoreUtils.put(KEY_USER_INFO, JSON.toJSONString(bean));
            StoreUtils.put(KEY_TOKEN, bean.getUserinfo().getToken());

        }
    }

    public void saveVisitorBean(LoginBean bean) {
        if (bean != null) {
            StoreUtils.put(KEY_VISITOR_INFO, JSON.toJSONString(bean));
            StoreUtils.put(KEY_TOKEN, bean.getUserinfo().getToken());

        }
    }

    public void clearUserBean() {
        StoreUtils.remove(KEY_USER_INFO);
        StoreUtils.remove(KEY_TOKEN);
    }

    public void clearVisitorBean() {
        StoreUtils.remove(KEY_VISITOR_INFO);
        StoreUtils.remove(KEY_TOKEN);
    }

    public boolean isLogin() {
        return StoreUtils.contains(KEY_TOKEN);

    }

    public boolean isRegister() {
        return StoreUtils.contains(KEY_USER_INFO);
    }

    public List<String> getSearchHistory() {
        String json = (String) StoreUtils.get(KEY_SEARCH_HISTORY, "[]");
        return JSONArray.parseArray(json, String.class);
    }

    public void saveSearchHistory(String history) {
        String json = (String) StoreUtils.get(KEY_SEARCH_HISTORY, "[]");
        List<String> list = JSONArray.parseArray(json, String.class);
        if (!list.contains(history)) {
            list.add(history);
            StoreUtils.put(KEY_SEARCH_HISTORY, JSON.toJSONString(list));
        }
    }

    public void clearSearchHistory() {
        StoreUtils.remove(KEY_SEARCH_HISTORY);
    }

}
